package recovery

import (
	infraStatus "aquaman/app/infra/grpc/status"
	"aquaman/app/infra/log"
	grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

// RecoveryInterceptor panic时返回Unknown错误吗
func Interceptor(logger log.CtxLogger) grpc_recovery.Option {
	return grpc_recovery.WithRecoveryHandler(func(p interface{}) (err error) {
		if err,ok := p.(error);ok {
			 return infraStatus.FromError(err).Err()
		}
		return status.Errorf(codes.Unknown, "panic triggered: %v", p)
	})
}